package com.power;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.ghtnativelib.GHTlibIf;
import com.idata.serialport.SerialPortControl;
import com.tools.dlog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class OtherPower {
   // 上层接口，在接口内实现具体的手持机平台上电，掉电方法
   // public static boolean isotherpowerup=false;

	private static final String TAG = "OtherPower";
	public static Context Ctt;
    //各手持机平台电源管理，需定义OpAddr地址
    //商米
    //public static String OpAddr = "/dev/ttyUHF1";
    //世麦 盛本
    //public static String OpAddr="/dev/ttyHSL3";
    //信通
    //public static String OpAddr="/dev/ttysWK0";
    //鸟鸟
    //public static String OpAddr="/dev/ttysWK1";
    //idata 95w 10
    //public static String OpAddr="/dev/ttyS0";
    //攀宁1上电
    // public static String OpAddr="/dev/ttysWK0";
    //slpb001
    //public static String OpAddr = "/dev/ttyS1";
    //攀宁2
    //public static String OpAddr="/dev/ttysWK2";
    //蓝畅
    //public static String OpAddr = "/dev/ttyS3:921600";
    //东极
    //public static String OpAddr="/dev/ttyHS0";
    //盈达 210802
    public static String OpAddr= "/dev/ttyS1";
    //东方拓宇 IWRIST R11
    //public static String OpAddr="/dev/ttyS1";
    //晶锐
    //public static String OpAddr="/dev/ttyMT1";


    //世麦  盛本 zoomsmart
   // static IUhfManager uhfManager  =null;

    //信通
   // static HcPowerCtrl ctrl = new HcPowerCtrl();
	public static boolean oPowerUp()
	{
		boolean rebl=true;
		//power_up();
		//new OtherPower().panningpowerup();
		//power_jingrui(true);
		//panning2_up();
		//nn_powerup();
		//return idata_95w_powerup();
		// handleRfidPower(true);
		//panningpowerup();
		//rebl=ydpowerup();

		new OtherPower().panling_power_up();
		//if(powerup_ght()==0)
			return true;
		//else
			//return false;
		//FileUtil.openRfidPower();
		//LCpower("1");
		//return rebl;
	}

	public static boolean oPowerDown()
	{
		boolean rebl=true;
		//power_down();
		//power_jingrui(false);
		//panning2_down();
		//nn_powerdown();
		//return idata_95w_powerdown();
		// handleRfidPower(false);

		//rebl=ydpowerdown();
		//panningpowerup();
		new OtherPower().panling_power_down();
		//if(powerdown_ght()==0)
			return true;
		//else
		//	return false;
		//FileUtil.closeRfidPower();
		//LCpower("0");
		//return rebl;
	}


    //鸟鸟
	/*public static UHFManager mUHFManager;
	public  static void nn_init()
	{
		mUHFManager = new UHFManager(Ctt.getApplicationContext());
	}
	public  static void nn_powerup()
	{
		mUHFManager.controlUHFPower(true,1);
	}
	public  static  void nn_powerdown()
	{
		mUHFManager.controlUHFPower(false,1);
	}
	*/

	/*
	public  static PowerControl nnpower=new PowerControl();
	public  static void nn_powerup()
	{
		nnpower._5VPowerControl(true);
	}
	public  static  void nn_powerdown()
	{
		nnpower._5VPowerControl(false);
	}
*/


	//idata 95w 10
	public static boolean idata_95w_powerup(){
		boolean bl= com.idata.rfid.SerialPortControl.controlIO("/dev/idata_uhf",0x05);
		try {
			Thread.sleep(1000);
		}catch (Exception ex)
		{}
		return  bl;
	}
	public static boolean idata_95w_powerdown()
	{
		return com.idata.rfid.SerialPortControl.controlIO("/dev/idata_uhf",0x06);
	}

	
	
	//肖邦新手持机上电？？？
	private void power_up() {
		   File com = new File("/sys/devices/platform/odm/odm:exp_dev/gps_com_switch");
		   File power = new File("/sys/bus/platform/devices/odm:exp_dev/psam_en");
		   dlog.toDlogAPI("power:" + power);
		  
		   writeFile(com, "1");
		   writeFile(power, "1");
		}
	
	private void power_down() {
		   File com = new File("/sys/devices/platform/odm/odm:exp_dev/gps_com_switch");
		   File power = new File("/sys/bus/platform/devices/odm:exp_dev/psam_en");
		   dlog.toDlogAPI("power:" + power);
		  
		   writeFile(com, "0");
		   writeFile(power, "0");
		}
	private synchronized void writeFile(File file, String value) {

		   try {
		      FileOutputStream outputStream = new FileOutputStream(file);
		      outputStream.write(value.getBytes());
		      outputStream.flush();
		      outputStream.close();

		   } catch (FileNotFoundException e) {
		      e.printStackTrace();
		   } catch (IOException e) {
		      e.printStackTrace();
		   }
		}
	//>>>>>>>>>>>>>>>>>>>>>>>

   //攀宁1上电
	// public static String OpAddr="/dev/ttysWK0";
	//*
	/*
	private PowerCtl powerCtl ;
	public void panningpowerup() {

		try {
			powerCtl = new PowerCtl();
			powerCtl.psam_ctl(1);
			powerCtl.sub_board_power(1);
			powerCtl.identity_ctl(1);
			powerCtl.scan_trig(1);
			powerCtl.scan_wakeup(0);
			powerCtl.scan_power(1);
			powerCtl.identity_uhf_power(1);
			powerCtl.uhf_ctl(1);

		} catch (SecurityException e) {
			//DisplayError(R.string.error_security);
		} catch (InvalidParameterException e) {
			//DisplayError(R.string.error_configuration);
		}
	}
	*/

	//*/
	
	//攀宁2
	/*public static String OpAddr="/dev/ttysWK2";//攀宁
	 private static PowerCtl powerCtl = new PowerCtl();
	private static void panning2_up()
	{
		powerCtl.identity_uhf_power(1);
		powerCtl.identity_ctl(1);
		powerCtl.uhf_ctl(1);
	}
	private static void panning2_down()
	{

		powerCtl.identity_uhf_power(0);
		powerCtl.identity_ctl(0);
		powerCtl.uhf_ctl(0);
	}
	 */
	//攀凌3上电
	private static final String vol33 = "/sys/class/rt5_gpio/gpio_uhf_ext_v33";
	private static final String vol5 = "/sys/class/rt5_gpio/gpio_uhf_ext_v5";
	private static final String power = "/sys/class/rt5_gpio/gpio_uhf_power";

	public void panling_power_up() {

		Log.d(TAG,"panling_power_up:");
		try {
			writeUhfFile(power, "1".getBytes());
			Thread.sleep(20);
			writeUhfFile(vol33, "1".getBytes());
			Thread.sleep(20);
			writeUhfFile(vol5, "1".getBytes());
			Thread.sleep(10);
		}catch(Exception ex){}


	}

	public void panling_power_down() {

		Log.d(TAG,"panling_power_down:");

		try {
			writeUhfFile(vol5, "0".getBytes());
			Thread.sleep(10);
			writeUhfFile(vol33, "0".getBytes());
			Thread.sleep(10);
			writeUhfFile(power, "1".getBytes());
			Thread.sleep(10);
		}catch(Exception ex){}
	}

	public static void writeUhfFile(String path, byte[] value) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(path);
			fos.write(value);
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			Log.d(TAG,"-----writeUhfFile-----e1=" + e.getMessage());
		} catch (IOException e) {
			Log.d(TAG,"-----writeUhfFile-----e2=" + e.getMessage());
			e.printStackTrace();
		}
	}
	//晶锐手持机上电
	public static final String KEY_RFIDIG_POWER_ACTION = "android.intent.action.RfidIgPower";
	private static void power_jingrui(boolean enable)
	{
		Intent intent = new Intent(KEY_RFIDIG_POWER_ACTION);
		intent.putExtra(KEY_RFIDIG_POWER_ACTION, enable);
		Ctt.sendBroadcast(intent);
	}

	//东方拓宇 IWRIST R11
	private static void handleRfidPower(boolean powerOn) {
		Intent pintent = new Intent("android.intent.action.SETTINGS_BJ");
		pintent.putExtra( "enable" , powerOn);
		Ctt.sendBroadcast(pintent);
	}


	//盈达 210802
	private static boolean ydpowerup()
	{
		return SerialPortControl.ioControl("/dev/uhf_dev",1);
	}
	private static boolean ydpowerdown()
	{
		return SerialPortControl.ioControl("/dev/uhf_dev",0);
	}

    //东极
	static GHTlibIf libIF=null;
	private static int powerup_ght()
	{
		int re;
		if(libIF==null)
			libIF=new GHTlibIf();
		re=libIF.InitLib();
		if(re!=0)
			return re;

		re= libIF.PowerControl(0);
		try {
			Thread.sleep(50);
		}catch(Exception ex){}
		return  re;
	}

	private static int powerdown_ght()
	{
		if(libIF==null)
			libIF=new GHTlibIf();

		int re=  libIF.PowerControl(1);
		try {
			Thread.sleep(1000);
		}catch(Exception ex){}
		return  re;
	}


    //蓝畅
	private static String s1 = "/proc/gpiocontrol/set_id";
	private static String s2 = "/proc/gpiocontrol/set_uhf";

	public static boolean LCpower(String state) {
		try {
			FileWriter localFileWriterOn = new FileWriter(s2);
			localFileWriterOn.write(state);
			localFileWriterOn.close();
			Log.e("PowerUtil", "power=" + state + " Path=" + s2);
			FileWriter RaidPower = new FileWriter(s1);
			RaidPower.write(state);
			RaidPower.close();
			Log.e("PowerUtil", "power=" + state + " Path=" + s1);
			Thread.sleep(200);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

    //商米
    /**关闭RFID服务*/
    static String BROADCAST_RFID_CLOSE = "com.sunmi.rfid.rfid_close";

   /**启用RFID服务*/
    static String BROADCAST_RFID_OPEN = "com.sunmi.rfid.rfid_open";
	final static String WAKE_STATE_STRING="/sys/class/sunmi_uhf/uhf/apint";
	static  boolean writeFileData(String path,String data)
    {
        try{
            FileWriter writer=new FileWriter(path);
            writer.write(data);
            writer.close();
            return true;
        }catch (Exception ex)
        {

        }
            return false;
    }

    public static  boolean  sunmipowerdown()
    {
        //关掉rfid服务
        Intent intent = new Intent(BROADCAST_RFID_OPEN);
        Ctt.sendBroadcast(intent);
        return  true;
    }
	public static  boolean  sunmipowerup()
    {
           //激活通信
        try {
            boolean bl = writeFileData(WAKE_STATE_STRING, "0");
            Log.d("ModuleAPI", " writeFileData "+WAKE_STATE_STRING+" 0");
            if (!bl)
                return false;
            Thread.sleep(10);

            bl = writeFileData(WAKE_STATE_STRING, "1");
            Log.d("ModuleAPI", " writeFileData "+WAKE_STATE_STRING+" 1");
            if (!bl)
                return false;
            Thread.sleep(10);
            bl = writeFileData(WAKE_STATE_STRING, "0");
            Log.d("ModuleAPI", " writeFileData "+WAKE_STATE_STRING+" 0");
            if (!bl)
                return false;
            Thread.sleep(10);

            //关掉rfid服务
            Log.d("ModuleAPI", "sendBroadcast "+BROADCAST_RFID_CLOSE);
             Intent intent = new Intent(BROADCAST_RFID_CLOSE);
            Ctt.sendBroadcast(intent);
            Thread.sleep(1000);
            return  true;
        } catch (InterruptedException e) {
            return  false;
        }
    }
}
